package com.cold.demo.rabbitmq.demo2.$4.direct;

import com.cold.demo.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import org.springframework.amqp.core.ExchangeTypes;

import java.nio.charset.StandardCharsets;

/**
 * @author: zyc
 */
public class Consumer4$1 {


    public static void main(String[] argv) throws Exception {
        argv = new String[]{"debug", "info", "error"};


        Connection connection = ConnectionUtils.getConnection();
        Channel    channel    = connection.createChannel();

        channel.exchangeDeclare(Publisher.exchange, "direct");
        // 当我们不向 queueDeclare() 提供任何参数时，我们将创建一个非持久的、独占的、自动删除的队列，并使用生成的名称：
        String queueName = channel.queueDeclare().getQueue();
        System.out.println("本次生成的队列名称为：" + queueName);
        // 将该生产的队列名称与交换机 logs 进行绑定
        for (String severity : argv) {
            System.out.println("绑定");
            AMQP.Queue.BindOk bindOk = channel.queueBind(queueName, Publisher.exchange, severity);
        }


        // 监听并消费消息
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println("RoutingKey：" + delivery.getEnvelope().getRoutingKey());
            System.out.println("消费消息：" + message);
        };


        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
            System.out.println("进来了");
        });

    }

}
